package com.sto.stoapp.activity.administrator;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.manager.GpsManager;
import com.sto.stoapp.manager.GpsManager.OnGpsChangedListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

public class AdminGpsInfomationActivity extends StoHomeActivity {

	private TextView gpsLongitudeText, gpsLatitudeText, gpsSpeedText,
			gpsElevationText, gpsDirectionText;

	private GpsManager gpsManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_infomation);
		this.setTitleBottom(R.string.gps_infomation,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		initListener();
	}

	private void initListener() {
		gpsManager.setOnGpsChangedListener(new OnGpsChangedListener() {

			@Override
			public void onGpsChangedListener(Location location) {
				if (location != null) {
					double latitude = location.getLatitude(); // 精度
					double longitude = location.getLongitude(); // 纬度
					double speed = location.getSpeed(); // 速度
					double altitude = location.getAltitude(); // 海拔
					double bearing = location.getBearing(); // 方位
					gpsLongitudeText.setText("" + longitude);
					gpsLatitudeText.setText("" + latitude);
					gpsSpeedText.setText("" + speed);
					gpsElevationText.setText("" + altitude);
					gpsDirectionText.setText("" + bearing);
				} else {
					Helper.showToast(AdminGpsInfomationActivity.this,
							getText(R.string.on_search_any_data).toString());
				}
			}
		});
	}

	private void initWidget() {
		gpsManager = GpsManager.getInstance(this);
		gpsLongitudeText = (TextView) findViewById(R.id.gps_longitude);
		gpsLatitudeText = (TextView) findViewById(R.id.gps_latitude);
		gpsSpeedText = (TextView) findViewById(R.id.gps_speed);
		gpsElevationText = (TextView) findViewById(R.id.gps_elevation);
		gpsDirectionText = (TextView) findViewById(R.id.gps_direction);
	}
	

	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
			gpsManager.closeGPS();
		
		super.onPause();
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		gpsManager.requestLocationUpdates();
		super.onResume();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 0 && resultCode == 0){
			if (!gpsManager.getLm().isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
				Toast.makeText(this, "GPS没有打开,请开启GPS！", Toast.LENGTH_SHORT).show();
			}else{
				gpsManager.initGps();
			}
		}

	}
	
}
